from flask import Flask, after_this_request

app = Flask(__name__)


# @app.before_first_request
# def before_first_request():
#     print("This function runs before the first request.")


@app.before_request
def before_request():
    print("This function runs before any request.")


@app.route("/welcome")
def welcome():
    return "Welcome！"


@app.route("/page/<int:page_id>")
def page(page_id):

    print(f"This is the page {page_id}.")
    # a = 0 / 0

    @after_this_request
    def after_this_request_func(response):
        print("This function runs after this request.")
        print(response.status_code)
        return response

    return f"hello, page id:{page_id}"


@app.after_request
def after_request(response):
    print("This function runs after every request.")
    return response


@app.teardown_request
def teardown_request(exception):

    print("This function runs after the request context is torn down.")
    print("exception11:", exception)


if __name__ == "__main__":
    app.run(debug=True, port=5012)
